<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- 课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <!-- 一级类别 开始-->
              <ul class="clearfix">
                <li
                  @click="clickOneSubject('')"
                  :class="{ current: !$route.query.subjectParentId }"
                >
                  <a title="全部" href="javascript:void(0);">全部</a>
                </li>
                <li
                  @click="clickOneSubject(item.id)"
                  :class="{ current: $route.query.subjectParentId === item.id }"
                  v-for="item in subjectList"
                  :key="item.id"
                >
                  <a :title="item.label" href="javascript:void(0);">{{
                    item.label
                  }}</a>
                </li>
              </ul>
              <!-- /一级类别 结束-->
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14" />
            </dt>
            <dd class="c-s-dl-li">
              <!-- 二级类别 开始-->
              <ul class="clearfix">
                <li
                  :class="{ current: !$route.query.subjectId }"
                  v-if="$route.query.subjectParentId"
                >
                  <a
                    title="全部"
                    href="javascript:void(0);"
                    @click="clickTwoSubject('')"
                    >全部</a
                  >
                </li>
                <li
                  @click="clickTwoSubject(item.id)"
                  :class="{ current: $route.query.subjectId === item.id }"
                  v-for="item in subjectTwoList"
                  :key="item.id"
                >
                  <a :title="item.label" href="javascript:void(0);">{{
                    item.label
                  }}</a>
                </li>
              </ul>
              <!-- /二级类别 结束-->
            </dd>
          </dl>
          <div class="clear" />
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <!-- 排序 开始 TODO-->
            <ol class="js-tap clearfix">
              <li
                :class="{ 'current bg-green': $route.query.salesCountOrder }"
                @click="clickSalesOrder(1)">
                <a title="销量" href="javascript:void(0);">销量
                  <!-- <i>↓</i> -->
                </a>
              </li>
              <li
                :class="{ 'current bg-green': $route.query.dateOrder }"
                @click="clickDateOrder(1)"
              >
                <a title="最新" href="javascript:void(0);">最新 </a>
              </li>
              <li
                @click="clickPriceOrder(1)"
                :class="{ 'current bg-green': $route.query.priceCountOrder }"
              >
                <a title="价格" href="javascript:void(0);">价格 </a>
              </li>
            </ol>
            <!-- /排序 结束-->
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="courseList.length === 0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam"
              >没有相关数据，小编正在努力整理中...</span
            >
          </section>
          <!-- /无数据提示 结束-->

          <!-- 数据列表 开始-->
          <article class="comm-course-list" v-if="courseList.length > 0">
            <ul id="bna" class="of">
              <li v-for="item in courseList" :key="item.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img
                      :alt="item.title"
                      :src="item.cover"
                      class="img-responsive"
                    />
                    <div class="cc-mask">
                      <a
                        :href="`/course/${item.id}`"
                        title="开始学习"
                        class="comm-btn c-btn-1"
                        >开始学习</a
                      >
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a
                      :href="`/course/${item.id}`"
                      :title="item.title"
                      class="course-title fsize18 c-333"
                      >{{ item.title }}</a
                    >
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green" v-if="item.price === 0">
                      <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                    <span class="fr jgTag">
                      <i class="c-orange fsize12 f-fA"> ￥{{ item.price }}</i>
                    </span>
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{ item.viewCount }}人学习</i>
                      |
                      <i class="c-999 f-fA">{{ item.buyCount }}人购买</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear" />
            <!-- 公共分页 开始 -->
              <div>
                <div class="paging">
                  <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
                  <a href="#" title="首页" @click="customPage(1)">首页</a>
                  <a href="#" title="前一页" @click="upPage">&lt;</a>
                  <a
                    href="#"
                    :title="`第${index + 1}页`"
                    :class="{ 'current undisable': index + 1 == pageBean.current }"
                    v-for="(item, index) in pages"
                    :key="index"
                    @click="customPage(index + 1)"
                    >{{ index + 1 }}</a
                  >
                  <a href="#" title="后一页" @click="nextPage">&gt;</a>
                  <a href="#" title="末页" @click="customPage(pages)"
                    >末</a
                  >
                  <div class="clear"></div>
                </div>
              </div>
              <!-- 公共分页 结束 -->

          </article>
          <!-- /数据列表 结束-->
        </div>
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>

<script>
import courseApi from "~/api/course/course";
import subjectApi from "~/api/subject/subject";
import querystring from "querystring";

export default {
  async asyncData(page) {
    // 查询全部课程条件
    let queryObj = {
      subjectId: page.query.subjectId || "",
      subjectParentId: page.query.subjectParentId || "",
      salesCountOrder: page.query.salesCountOrder || "",
      dateOrder: page.query.dateOrder || "",
      priceCountOrder: page.query.priceCountOrder || "",
    };
    let pageBean = {
      current: page.query.current || 1,
      size: page.query.size || 8
    }
    // 总页数参数
    let pages = 1
    // 查询全部的课程
    let courses = await courseApi.getList(pageBean.current, pageBean.size, queryObj);
    let courseList = courses.data.records
    pages = courses.data.pages

    // 查询所有的科目
    let subjects = await subjectApi.getList();
    let subjectList = subjects.data;
    // 查询二级科目
    let subjectTwoList = [];
    for (let i = 0; i < subjectList.length; i++) {
      if (subjectList[i].id === page.query.subjectParentId) {
        subjectTwoList = subjectList[i].children;
        console.log(subjectList[i].children);
      }
    }
    return {
      // 课程
      courseList,
      // 全部分类
      subjectList,
      // 二级科目分类
      subjectTwoList,
      // 课程查询条件
      queryObj,
      // 分页条件
      pageBean,
      // 分页参数
      pages
    };
  },
  methods: {
    // 选择一级分类
    clickOneSubject(id) {
      // 响应到url中
      window.location = `?subjectParentId=${id}`;
    },
    // 选择二级分类
    clickTwoSubject(id) {
      window.location = `?subjectParentId=${this.$route.query.subjectParentId}&subjectId=${id}`;
    },
    // 销量排序
    clickSalesOrder(id) {
      this.queryObj.salesCountOrder = id;
      this.queryObj.type = !this.queryObj.type
      this.queryObj.priceCountOrder = "";
      this.queryObj.dateOrder = "";
      // 拼接url，优化
      let data = querystring.stringify(this.queryObj);
      window.location = "course?" + data;
    },
    // 价格排序
    clickPriceOrder(id) {
      this.queryObj.priceCountOrder = id;
      this.queryObj.dateOrder = "";
      this.queryObj.salesCountOrder = "";
      // 拼接url，优化
      let data = querystring.stringify(this.queryObj);
      window.location = "course?" + data;
    },
    // 日期排序
    clickDateOrder(id) {
      this.queryObj.dateOrder = id;
      this.queryObj.salesCountOrder = "";
      this.queryObj.priceCountOrder = "";
      // 拼接url，优化
      let data = querystring.stringify(this.queryObj);
      window.location = "course?" + data;
    },
    // 下一页
    nextPage() {
      if (this.pageBean.current >= this.pages) {
        return false;
      }
      this.pageBean.current = Number(this.pageBean.current) + 1;
      let data = querystring.stringify(this.queryObj);
      let data2 = querystring.stringify(this.pageBean);
      window.location = "course?" + data + "&" + data2;
    },
    // 上一页
    upPage() {
      if (this.pageBean.current <= 1) {
        return false;
      }
      this.pageBean.current = Number(this.pageBean.current) - 1;
      let data = querystring.stringify(this.queryObj);
      let data2 = querystring.stringify(this.pageBean);
      window.location = "course?" + data + "&" + data2;
    },
    // 跳转指定页数
    customPage(current) {
      // 拼接url，跳转
      this.pageBean.current = current
      let data = querystring.stringify(this.queryObj);
      let data2 = querystring.stringify(this.pageBean);
      window.location = "course?" + data + "&" + data2;
    },
  },
};
</script>
